/**
 * jims
 */
package com.jims.master.modules.med.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.sys.entity.Company;
import com.jims.master.modules.sys.service.CompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.med.entity.OrgVsProvince;
import com.jims.master.modules.med.service.OrgVsProvinceService;

import java.util.List;

/**
 * 组织机构与省份对照Controller
 * @author 陈晓阳
 * @version 2017-06-06
 */
@Controller
@RequestMapping(value = "${adminPath}/med/orgVsProvince")
public class OrgVsProvinceController extends BaseController {

	@Autowired
	private OrgVsProvinceService orgVsProvinceService;
    @Autowired
    private CompanyService companyService;
	
	@ModelAttribute
	public OrgVsProvince get(@RequestParam(required=false) String id) {
		OrgVsProvince entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = orgVsProvinceService.get(id);
		}
		if (entity == null){
			entity = new OrgVsProvince();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OrgVsProvince orgVsProvince, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OrgVsProvince> page = orgVsProvinceService.findPage(new Page<OrgVsProvince>(request, response), orgVsProvince); 
		model.addAttribute("page", page);
		return "modules/med/orgVsProvinceIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(OrgVsProvince orgVsProvince, Model model) {
		model.addAttribute("orgVsProvince", orgVsProvince);
        List<Company> companyList=companyService.findList();
        model.addAttribute("companyList", companyList);
		return "modules/med/orgVsProvinceForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OrgVsProvince orgVsProvince, Model model) {
	    StringData data=new StringData();
		int i=orgVsProvinceService.save(orgVsProvince);
        if(i>0){
            data.setCode("success");
            data.setData("保存组织机构与省份对照成功");
        }else{
            data.setCode("error");
            data.setData("保存组织机构与省份对照失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OrgVsProvince orgVsProvince) {
	    StringData data=new StringData();
		int i=orgVsProvinceService.delete(orgVsProvince);
        if(i>0){
            data.setCode("success");
            data.setData("删除组织机构与省份对照成功");
        }else{
            data.setCode("error");
            data.setData("删除组织机构与省份对照失败");
        }
		return data;
	}

}